package edu.biwu.filter02;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.annotation.WebInitParam;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;

@WebFilter(filterName = "charseFilter",urlPatterns = "/*",initParams = @WebInitParam(name = "charset",value = "UTF-8"))
//@WebFilter("/*")
public class CharseFilter implements Filter {
    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

        //根据过滤器的名称获取已注册的过滤器
        FilterRegistration charseFilter = request.getServletContext().getFilterRegistration("charseFilter");

        //根据参数name取出过滤器中的值
        String charset = charseFilter.getInitParameter("charset");

        //response.setContentType("text/html;charset=utf-8");
        response.setContentType("text/html;charset="+charset);

        chain.doFilter(request, response);
    }
}
